//
//  TransactionDetailsController.swift
//  newshaohuo
//
//  Created by shaohuo on 2018/2/6.
//  Copyright © 2018年 shaohuo. All rights reserved.
//

import Foundation
import MJRefresh

class TransactionDetailsController: UITableViewController {
    
    var page : Int!
    var totalPage : Int!
    
    var transactionArray = NSMutableArray()
    
    let cellIdentifier = "TransactionDetailsCell"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "交易明细"
        
        loadTableView()
        
        refresh()
        tableView.mj_header.beginRefreshing()
        
    }
   
    private func loadTableView() {
        
        tableView.tableFooterView = UIView()
        tableView.register(UINib.init(nibName: "TransactionDetailsCell", bundle: nil), forCellReuseIdentifier: cellIdentifier)
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 100
        
    }
    
    func refresh() {
        
        // 顶部刷新
        let header = MJRefreshNormalHeader()
        header.setRefreshingTarget(self, refreshingAction: #selector(headerRefresh))
        tableView.mj_header = header
        
        // 底部刷新
        let footer = MJRefreshAutoNormalFooter()
        footer.setRefreshingTarget(self, refreshingAction: #selector(footerRefresh))
        tableView.mj_footer = footer
        
    }
    
    // 顶部刷新
    @objc fileprivate func headerRefresh(){
        page = 1
        requestGetUserExpenceLogNew(current_page: page)
    }
    // 底部刷新
    @objc fileprivate func footerRefresh(){
        
        if page<=totalPage{
            requestGetUserExpenceLogMore(current_page: page)
        }else {
            self.tableView.mj_footer.endRefreshing()
        }
    }
    
    //交易明细
    func requestGetUserExpenceLogNew(current_page:Int) -> Void {
        
        //要加密的数据
        let param = ["command":"getUserExpenceLog","token":UserTicketModel.sharedInstance.token ?? "","uid":UserTicketModel.sharedInstance.uid ?? "","current_page":current_page,"app_name":"shr"] as [String : Any]
        
        let network = NetworkTools.sharedInstance
        network.postRequest(urlString: "", parameters: param as AnyObject, headers: ["copyright":"v1","interface":"UserInfo"], successed: { (resultDict) in
            self.tableView.mj_header.endRefreshing()
            
            let jsonString = network.getJSONStringFromDictionary(dictionary: (resultDict["data"] as? NSDictionary)!)
            
            guard let jsonData = jsonString.data(using: String.Encoding.utf8) else {return}
            do {
                let objc: TransactionItem = try JSONDecoder().decode(TransactionItem.self, from: jsonData)
                self.page = objc.current_page+1
                self.totalPage = objc.total
                self.transactionArray.removeAllObjects()
                let alittles : [TransactionDetailsItem] = objc.data
                
                for ali in alittles {
                    self.transactionArray.add(ali)
                }
                self.tableView.reloadData()
                
            }catch {
                print(error)
            }
            
        })
    }
    
    func requestGetUserExpenceLogMore(current_page:Int) -> Void {
        
        //要加密的数据
        let param = ["command":"getUserExpenceLog","token":UserTicketModel.sharedInstance.token ?? "","uid":UserTicketModel.sharedInstance.uid ?? "","current_page":current_page,"app_name":"shr"] as [String : Any]
        let network = NetworkTools.sharedInstance
        network.postRequest(urlString: "", parameters: param as AnyObject, headers: ["copyright":"v1","interface":"UserInfo"], successed: { (resultDict) in
            self.tableView.mj_footer.endRefreshing()
            
            let jsonString = network.getJSONStringFromDictionary(dictionary: (resultDict["data"] as? NSDictionary)!)
            
            guard let jsonData = jsonString.data(using: String.Encoding.utf8) else {return}
            do {
                let objc: TransactionItem = try JSONDecoder().decode(TransactionItem.self, from: jsonData)
                self.page = objc.current_page+1
                self.totalPage = objc.total
                let alittles : [TransactionDetailsItem] = objc.data
            
                for ali in alittles {
                    self.transactionArray.add(ali)
                }
                self.tableView.reloadData()
                
            }catch {
                print(error)
            }
            
        })
    }

}

extension TransactionDetailsController {
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return transactionArray.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! TransactionDetailsCell
        
        cell.selectionStyle = .none
        
        let transaItem = self.transactionArray[indexPath.row] as! TransactionDetailsItem
        cell.transaItem = transaItem
        
        return cell
        
    }
    
}
